package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Staff;

import admin.models.StaffModel;

public class ProcessStaffServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(ProcessStaffServlet.class.getName());
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		log.setLevel(Level.FINE);
		
		String strIds = (String) request.getParameter("ids");
	    String action = (String) request.getParameter("action");
	    int status = 0;
	    String[] ids = strIds.split(",");
	    StaffModel staffModel = StaffModel.getInstance();
	    
	    ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("status_active", "status_inactive"));
	    
	    for (int i = 0; i < ids.length; i++) {
	        if (!ids[i].isEmpty()) {
	        	if(statusAction.contains(action.toLowerCase())) {
	                if (action.equalsIgnoreCase("status_active")) {
	                	status = 1;
					} else if (action.equalsIgnoreCase("status_inactive")) {
						status = 0;
					}
	                
	                Staff user = staffModel.getUser(ids[i]);
	                if (user != null) {
	        			user.setStatus(status);
	        		}
	                staffModel.insert(user);
	                log.info("Change status staff to " + action + " with username : " + user.getUsername());
	        	} else if (action.equalsIgnoreCase("delete")) {
	        		Staff user = staffModel.getUser(ids[i]);
	        		staffModel.delete(user);
	        		log.info("Delete username : " + user.getUsername());
				}
	        }
	    }
	}
}
